package com.fowo.api.common.model;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fowo.api.common.util.ListUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 表示一个字符串或一个字段串列表
 */
@JsonSerialize(using = DataIndex.DataIndexJsonSerializer.class)
@JsonDeserialize(using = DataIndex.DataIndexJsonDeserializer.class)
public class DataIndex {
    private List<String> value;

    public DataIndex(List<String> value) {
        this.value = value;
    }

    public DataIndex(String... values) {
        value = Arrays.asList(values);
    }

    public List<String> getValue() {
        return value;
    }

    @Override
    public String toString() {
        return ListUtils.join(",", value);
    }

    public String toFieldName() {
        return ListUtils.join("_", value);
    }

    public static DataIndex of(String value) {
        if(StringUtils.hasText(value)) {
            return new DataIndex(value.split(","));
        }
        return new DataIndex();
    }

    static class DataIndexJsonDeserializer extends JsonDeserializer<DataIndex> {

        @Override
        public DataIndex deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
            JsonToken token = jsonParser.currentToken();
            if (token == null) {
                return null;
            }
            if (token == JsonToken.VALUE_STRING) {
                DataIndex dataIndex = new DataIndex(jsonParser.getValueAsString());
//                jsonParser.nextToken();
                return dataIndex;
            }
            if (token == JsonToken.START_ARRAY) {
                List<String> values = new ArrayList<>();
                while((token = jsonParser.nextToken()) != JsonToken.END_ARRAY) {
                    values.add(jsonParser.getValueAsString());
                }
//                jsonParser.nextToken();
                return new DataIndex(values);
            }
            return null;
        }
    }

    static class DataIndexJsonSerializer extends JsonSerializer<DataIndex> {

        @Override
        public void serialize(DataIndex dataIndex, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if(dataIndex.value.isEmpty()) {
                jsonGenerator.writeNull();
                return;
            }
            if(dataIndex.value.size() == 1) {
                jsonGenerator.writeString(dataIndex.value.get(0));
                return;
            }
            jsonGenerator.writeStartArray();
            for(String item : dataIndex.value) {
                jsonGenerator.writeString(item);
            }
            jsonGenerator.writeEndArray();
        }
    }
}
